// i.d(t, { p: () => PortalPuckMesh, Y: () => g })
import * as THREE from "three"
import r from "../shader/22118"
import a from "../shader/28100"
class PortalMaterial extends THREE.RawShaderMaterial {
  static uniforms: {
    tNoHover: { type: string; value: any }
    tHover: { type: string; value: any }
    tPortal: { type: string; vlaue: any }
    progress: { type: string; value: number }
    opacity: { type: string; value: number }
  }
  constructor(e, t) {
    const i = THREE.UniformsUtils.clone(PortalMaterial.uniforms)
    i.tNoHover.value = e
    i.tHover.value = t
    i.tPortal.value = null
    super({
      vertexShader: r,
      fragmentShader: a,
      uniforms: i,
      name: "PortalMaterial",
      transparent: !0
    })
  }
}
PortalMaterial.uniforms = {
  tNoHover: { type: "t", value: null },
  tHover: { type: "t", value: null },
  tPortal: { type: "t", vlaue: null },
  progress: { type: "f", value: 1 },
  opacity: { type: "f", value: 1 }
}
import TextureLoader from "../loader/TextureLoader"

let p
const m = {
  get: () =>
    p ||
    ((p = {
      toExteriorTexture: TextureLoader("./images/exterior.png"),
      toExteriorHoverTexture: TextureLoader("./images/exterior_hover.png"),
      toInteriorTexture: TextureLoader("./images/interior.png"),
      toInteriorHoverTexture: TextureLoader("./images/interior_hover.png")
    }),
    p)
}

import y from "../packages/89352"
import meshStateEnum from "../enum/meshState.enum"
import renderOrderEnum from "../enum/renderOrder.enum"
import funcUtil from "../utils/func.util"
import CameraData from "../data/CameraData"
export default class PortalPuckMesh extends THREE.Mesh<THREE.PlaneGeometry, PortalMaterial> {
  static geometry: THREE.PlaneGeometry
  uniforms: { [uniform: string]: THREE.IUniform<any> }
  portalData: any
  hoverProgress: number
  hovered: boolean
  constructor(e, t) {
    const i = new PortalMaterial(m.get().toInteriorTexture, m.get().toInteriorHoverTexture)
    super(PortalPuckMesh.geometry, i)
    this.layers.mask = t.mask
    this.uniforms = i.uniforms
    this.renderOrder = renderOrderEnum.portals
    this.setState(meshStateEnum.HIDE)
    this.update(e)
  }
  update(e) {
    this.portalData = e
    this.position.copy(e.position)
    if (null !== e.lookDirection) {
      const t = e.lookDirection.clone().add(e.position)
      this.lookAt(t)
    }
    this.hoverProgress = 0
    this.hovered = !1
    e.toExterior
      ? ((this.uniforms.tNoHover.value = m.get().toExteriorTexture), (this.uniforms.tHover.value = m.get().toExteriorHoverTexture))
      : ((this.uniforms.tNoHover.value = m.get().toInteriorTexture), (this.uniforms.tHover.value = m.get().toInteriorHoverTexture))
  }
  resetMesh(e = 0, t = !1, i = !1) {
    this.uniforms.opacity.value = e
    this.visible = 0 !== e
    this.material.depthTest = t
    this.material.depthWrite = i
  }
  setHover(e: boolean) {
    this.hovered = e
  }
  setState(e: number) {
    switch (e) {
      case meshStateEnum.HIDE:
        this.resetMesh(0, !0, !0)
        break
      case meshStateEnum.SHOW:
        this.resetMesh(1, !0, !0)
        break
      case meshStateEnum.ONTOP:
        this.resetMesh(1)
    }
  }
  render(e: number, t: CameraData) {
    this.hovered && this.hoverProgress < 1 ? (this.hoverProgress += e / 300) : !this.hovered && this.hoverProgress > 0 && (this.hoverProgress -= e / 300)
    this.hoverProgress = funcUtil.getMax(this.hoverProgress, 0, 1)
    this.uniforms.progress.value = this.hoverProgress
    if (this.portalData?.billboard) {
      const e = t.pose.position
      const i = this.position.copy(this.portalData.position)
      const n = e.distanceTo(i)
      n < y.nm ? i.lerpVectors(e, i, y.nm / n) : n > y.iz && i.lerpVectors(e, i, y.iz / n), this.lookAt(t.pose.position)
    }
  }
  updatePortalTexture(e: THREE.Texture) {
    this.uniforms.tPortal.value = e
  }
}
PortalPuckMesh.geometry = new THREE.PlaneGeometry(y.vX, y.vX)
